<template>
  <div class="overtime-approve-container">
    <el-card class="page-card" shadow="hover">
      <template #header>
        <div class="card-header">
          <div class="header-left">
            <el-icon class="header-icon"><Check /></el-icon>
            <span class="header-title">审批加班</span>
          </div>
        </div>
      </template>
      
      <el-table 
        :data="overtimeList" 
        style="width: 100%"
        stripe
        :empty-text="'暂无待审批的加班申请'"
      >
        <el-table-column prop="userId" label="员工ID" width="100" align="center" />
        <el-table-column prop="type" label="类型" width="120" align="center">
          <template #default="scope">
            <el-tag type="warning" effect="dark">
              <el-icon><Timer /></el-icon>
              加班
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="申请时间" width="180" align="center">
          <template #default="scope">
            <span class="time-text">{{ formatDate(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态" width="100" align="center">
          <template #default="scope">
            <el-tag type="warning">待审批</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180" align="center" fixed="right">
          <template #default="scope">
            <el-button size="small" type="success" @click="handleApprove(scope.row.id, 'APPROVED')">
              <el-icon><Check /></el-icon>
              通过
            </el-button>
            <el-button size="small" type="danger" @click="handleApprove(scope.row.id, 'REJECTED')">
              <el-icon><Close /></el-icon>
              拒绝
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
import { ref, onMounted } from 'vue'
import request from '../utils/request'
import { ElMessage } from 'element-plus'
import { Check, Timer, Close } from '@element-plus/icons-vue'

export default {
  name: 'OvertimeApprove',
  components: {
    Check,
    Timer,
    Close
  },
  setup() {
    const overtimeList = ref([])
    
    const formatDate = (dateStr) => {
      if (!dateStr) return ''
      return new Date(dateStr).toLocaleString('zh-CN')
    }
    
    const loadOvertimeList = async () => {
      try {
        const res = await request.get('/attendance/overtime/pending')
        overtimeList.value = res.data || []
      } catch (error) {
        ElMessage.error('加载加班申请失败')
      }
    }
    
    const handleApprove = async (recordId, status) => {
      try {
        await request.post('/attendance/overtime/approve', {
          recordId: recordId,
          status: status
        })
        ElMessage.success('审批完成')
        loadOvertimeList()
      } catch (error) {
        ElMessage.error(error.message || '审批失败')
      }
    }
    
    onMounted(() => {
      loadOvertimeList()
    })
    
    return {
      overtimeList,
      formatDate,
      handleApprove
    }
  }
}
</script>

<style scoped>
.overtime-approve-container {
  width: 100%;
  height: 100%;
}

.page-card {
  border-radius: 12px;
  border: none;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 12px;
}

.header-icon {
  font-size: 24px;
  color: #409eff;
}

.header-title {
  font-size: 20px;
  font-weight: 600;
  color: #1a1a1a;
}

.time-text {
  color: #666;
  font-size: 14px;
}

:deep(.el-table) {
  border-radius: 8px;
  overflow: hidden;
}

:deep(.el-table th) {
  background-color: #fafafa;
  color: #333;
  font-weight: 600;
}

:deep(.el-table td) {
  padding: 16px 0;
}
</style>

